import { NextRequest, NextResponse } from "next/server"; import { createReadStream, existsSync, statSync } from "node:fs"; import path from "node:path"; import { STORAGE_DIR } from "@/lib/listings/media"; export const dynamic = "force-dynamic"; /** Sert les variantes WebP depuis le stockage local (clé validée, pas de traversée de répertoire). */ export async function GET(_req: NextRequest, { params }: { params: Promise<{ key: string[] }> }) { const { key } = await params; if (key.length !== 3 || !/^\d{4}-\d{2}$/.test(key[0]) || !/^[a-f0-9]{24}$/.test(key[1]) || !/^(1600|800|320)\.webp$/.test(key[2])) return new NextResponse("Not found", { status: 404 }); const p = path.join(STORAGE_DIR, key[0], key[1], key[2]); if (!p.startsWith(STORAGE_DIR) || !existsSync(/*turbopackIgnore: true*/ p)) return new NextResponse("Not found", { status: 404 }); const size = statSync(p).size; const stream = createReadStream(p); return new NextResponse(stream as unknown as ReadableStream, { headers: { "content-type": "image/webp", "content-length": String(size), "cache-control": "public, max-age=31536000, immutable" } }); }